<?php

declare(strict_types=1);

namespace app\admin\controller;

use addons\Builder\facade\Builder;
use think\Response;
use app\common\tool\Result;
use think\facade\View;
use think\response\Json;
use app\common\model\Cate;
use app\common\model\Exam;
use app\common\model\PaperBank;
//该文件为自动生成，请按需修改
class Paper extends Base
{
    //主键，默认为id
    protected string $pk = 'id';

    //主模型
    protected string $model = '\app\common\model\Paper';

    protected $append = ['cateText'];
    public function index(): string
    {
        return Builder::Table()
            ->autoStructure($this->model, $this->pk, 'barDemo,200')
            ->addTopButtons([
                'add' => ['size' => '60%'],
            ])
            ->addRightButtons(
                [
                    'add_paper' => [
                        'size' => '100%',
                        'title' => '添加考题',
                        'url' => url('paperBank/index')->build(),
                        'event' => '_openDrawer'
                    ],
                    'edit'  => ['size' => '50%'],
                    'delete'
                ]
            )
            ->addSearch([
                ['title', '考卷名称', 'input,Like'],
                ['cate', '所属分类', 'select', self::getCate()]
            ])
            ->fetch();
    }
    public function add($tpl = 'paper/edit')
    {
        $cate = Cate::where('pid', 0)->order('sort asc')->select();

        view::assign(
            [
                'cate' => $cate,
            ]
        );
        return parent::add($tpl); // TODO: Change the autogenerated stub
    }
    public function edit($id = '', $tpl = 'paper/edit', $info = '')
    {
        $cate = Cate::where('pid', 0)->order('sort asc')->select();
        $info = $this->model::find($id);
        view::assign(
            [
                'cate' => $cate,
            ]
        );
        return parent::edit($id, $tpl, $info); // TODO: Change the autogenerated stub
    }

    /**
     * 获取分类
     */
    public static function getCate()
    {
        $cate = Cate::where('pid', 0)->order('sort asc')->column('title', 'id');
        return $cate;
    }

    public function delete(): Response
    {
        try {
            $param = $this->request->post();
            return json($this->model::paperDelete($param, $this->getLogsArguments($param)));
        } catch (\Exception $e) {
            return json(['code' => -1, 'msg' => $e->getMessage()]);
        }
    }
    public function add_paper()
    {
       
        $id = $this->request->param('id');
        $info = $this->model::find($id);
        if($this->request->isPost()){
            $param = $this->request->param();
            try{
                return json($this->model::add_paper($param));
            }catch (\Exception $e) {
                return json(['code' => -1, 'msg' => $e->getMessage()]);
            }
        }
        $radio_data = Exam::where(['types'=>1,'cate'=>$info['cate']])->field('id,title')->select();
        foreach ($radio_data as $k=>$value){
            $PaperBank = PaperBank::where(['cate'=>$info['cate'],'paper_id'=>$id,'exam_id'=>$value['id']])->count();
            $value['check'] = $PaperBank>0?1:0;
            $radio_data[$k]=$value;
        }
        $check_data = Exam::where(['types'=>2,'cate'=>$info['cate']])->field('id,title')->select();
        foreach ($check_data as $k=>$value){
            $PaperBank = PaperBank::where(['cate'=>$info['cate'],'paper_id'=>$id,'exam_id'=>$value['id']])->count();
            $value['check'] = $PaperBank>0?1:0;
            $check_data[$k]=$value;
        }
        View::assign('info', $info);
        View::assign('radio_data', $radio_data);
        View::assign('check_data', $check_data);
        return View::fetch('paper/add_paper');
    }
}
